const { defineConfig } = require("@vue/cli-service");
const Mock = require("mockjs");
const { list } = require("./src/mock");
const bodyParser = require("body-parser");
const JWT = require("jsonwebtoken");
const key = 'qoawhdnwklidjqlw;oq'
const userList = [
  {
    username: "123456@163.com",
    password: "123456789",
    nickname: "在不疯狂就老了",
    avator: Mock.Random.image("30x30", Mock.Random.color()),
  },
  {
    username: "13171996138",
    password: "04549685",
    nickname: "专业碰瓷",
    avator: Mock.Random.image("30x30", Mock.Random.color()),
  },
  {
    username: "666666@qq.com",
    password: "123",
    nickname: "六六楼",
    avator: Mock.Random.image("30x30", "@color"),
  },
];
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middleware, { app }) {
      app.use(bodyParser.json());
      // 选车数据
      app.get("/api/car/random/:count", (req, res) => {
        const { count } = req.params;

        const { list } = Mock.mock({
          ["list|" + count]: [
            {
              url: "@image(100x100, @color)",
              title: "@ctitle",
              desc: "@cword(10, 20)",
              id: "@id",
            },
          ],
        });

        res.send({
          code: 200,
          data: list,
        });
      });

      // 首页数据
      app.get("/api/home/list", (req, res) => {
        const { pageCode = 1, pageSize = 10 } = req.query;

        if (list.length >= pageCode * pageSize) {
          res.send({
            code: 200,
            data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
            message: "数据加载成功!",
          });
        } else {
          res.send({
            code: 201,
            data: [],
            message: "没有更多数据了！",
          });
        }
      });

      // 搜索接口
      app.get("/api/search", (req, res) => {
        const { searchValue } = req.query;
        const filterList = list.filter((v) => v.title.includes(searchValue));
        res.send({
          code: 200,
          data: filterList,
        });
      });

      // 详情接口
      app.get("/api/detail", (req, res) => {
        const { id } = req.query;
        const item = list.find((v) => v.id === id);
        if (item) {
          res.send({
            code: 200,
            data: item,
          });
        } else {
          res.send({
            code: 404,
            data: null,
            message: "商品已经下架",
          });
        }
      });

      // 登录接口
      app.post("/api/login", (req, res) => {
        const { username, password } = req.body;
        const user = userList.find((v) => {
          return v.username === username && v.password === password;
        });

        if (user) {
          res.send({
            code: 200,
            message: "登录成功",
            data: {
              nickname: user.nickname,
              avator: user.avator,
              username: user.username,
              token: JWT.sign({
                nickname: user.nickname,
                avator: user.avator,
                username: user.username,
              }, key),
            },
          });
        } else {
          res.send({
            code: 401,
            message: "账户密码错误， 请重新登录",
          });
        }
      });

      // 评论接口
      app.post('/api/comment/add', (req, res) => {
        const { id, comment } = req.body;
        const item = list.find(v => v.id === id)
        item.comment.unshift({
          ...comment,
          createTime: new Date().toLocaleString(),
          id: Mock.Random.id()
        })

        res.send({
          code: 200,
        })
      })

      return middleware;
    },
  },
});
